package com.example.campuslink.commen.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreDialog
import com.example.campuslink.commen.db.school.SchoolInfoDao
import com.example.campuslink.commen.db.school.SchoolInfoData
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.commen.enums.SchoolProvinceEnums
import com.example.campuslink.databinding.DialogAddSchoolBinding
import com.example.campuslink.ui.activity.SchoolChoiceActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference

class AddSchoolDialog : CoreDialog<DialogAddSchoolBinding>(R.layout.dialog_add_school) {
    private val provinceList = SchoolProvinceEnums.chineseNameList()
    private lateinit var adapter: ArrayAdapter<String>
    private var province = SchoolProvinceEnums.BEIJING.name
    var imgPath: String = "https://static-data.gaokao.cn/upload/logo/472.jpg"
    private var activityRef: WeakReference<SchoolChoiceActivity>? = null
    private lateinit var schoolInfoData: SchoolInfoData
    private lateinit var schoolInfoDao: SchoolInfoDao
    fun upload() {
        binding.tvChoiceSchoolImg.text = "上传成功"
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        activityRef = WeakReference(context as SchoolChoiceActivity)
        adapter = ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, provinceList)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item)
        schoolInfoData = SchoolInfoData.getDatabase(context)
        schoolInfoDao = schoolInfoData.schoolInfoDao()
    }

    override fun setupViews() {
        binding.spinnerShowProvince.adapter = adapter
        binding.spinnerShowProvince.setOnItemSelectedListener(object :
            AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                province = provinceList[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        })

        binding.btnCancel.setOnClickListener {
            dismiss()
        }

        binding.tvChoiceSchoolImg.setOnClickListener {
            activityRef?.get()?.openGallery()
        }

        binding.btnConfirm.setOnClickListener {
            if (binding.etSchoolName.text.isNotEmpty() && binding.etSchoolContent.text.isNotEmpty()) {
                addSchool()
                lifecycleScope.launch(Dispatchers.Main) {
                    Toast.makeText(requireContext(), "添加成功", Toast.LENGTH_SHORT).show()
                    dismiss()
                    activityRef?.get()?.notifySchoolAdapter()
                }
            } else {
                Toast.makeText(requireContext(), "请填写完整信息", Toast.LENGTH_SHORT).show()
            }
        }
    }


    private fun addSchool() {
        lifecycleScope.launch(Dispatchers.IO) {
            val schoolName = binding.etSchoolName.text.toString()
            val schoolContent = binding.etSchoolContent.text.toString().split("、")
            val schoolInfo = SchoolInfoTable()
            schoolInfo.schoolName = schoolName
            schoolInfo.schoolIntroduce = schoolContent.toMutableList()
            schoolInfo.province = province
            schoolInfo.schoolImage = imgPath
//            schoolInfo.searchData = mutableListOf()
//            schoolInfo.messageList = mutableListOf()
//            schoolInfo.mainInvitationList = mutableListOf()
//            schoolInfo.shoppingInvitationList = mutableListOf()
//            schoolInfoDao.insertSchoolInfo(schoolInfo)
            if (schoolInfoDao?.getSchoolInfoBySchoolName(schoolName) == null) {
                schoolInfoDao.insertSchoolInfo(schoolInfo)
            } else {
                lifecycleScope.launch(Dispatchers.Main) {
                    Toast.makeText(context, "该学校已存在", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }


    override fun createBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): DialogAddSchoolBinding {
        return DialogAddSchoolBinding.inflate(inflater, container, false)
    }
}